package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.archive.domain.SDisabledSoldier;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.service.ISTransferredCadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.SDisabledMilitiamanMapper;
import com.ruoyi.archive.domain.SDisabledMilitiaman;
import com.ruoyi.archive.service.ISDisabledMilitiamanService;

/**
 * 伤残民兵民工信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-30
 */
@Service
public class SDisabledMilitiamanServiceImpl extends ServiceImpl<SDisabledMilitiamanMapper, SDisabledMilitiaman> implements ISDisabledMilitiamanService {
    @Autowired
    private SDisabledMilitiamanMapper sDisabledMilitiamanMapper;

    @Autowired
    private ISTransferredCadresService transferredCadresService;

    /**
     * 查询伤残民兵民工信息
     *
     * @param id
     *         伤残民兵民工信息主键
     *
     * @return 伤残民兵民工信息
     */
    @Override
    public SDisabledMilitiaman selectSDisabledMilitiamanById(Long id) {
        return sDisabledMilitiamanMapper.selectSDisabledMilitiamanById(id);
    }

    /**
     * 查询伤残民兵民工信息列表
     *
     * @param sDisabledMilitiaman
     *         伤残民兵民工信息
     *
     * @return 伤残民兵民工信息
     */
    @Override
    public List<SDisabledMilitiaman> selectSDisabledMilitiamanList(SDisabledMilitiaman sDisabledMilitiaman) {
        List<SDisabledMilitiaman> disabledMilitiamanList = sDisabledMilitiamanMapper.selectSDisabledMilitiamanList(sDisabledMilitiaman);
        if (CollectionUtil.isNotEmpty(disabledMilitiamanList)) {
            List<Long> soldierIdList = disabledMilitiamanList.stream().map(SDisabledMilitiaman::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = transferredCadresService.getSoldierBaseInfoMap(soldierIdList);
            disabledMilitiamanList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            disabledMilitiamanList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return disabledMilitiamanList;
    }

    /**
     * 新增伤残民兵民工信息
     *
     * @param sDisabledMilitiaman
     *         伤残民兵民工信息
     *
     * @return 结果
     */
    @Override
    public int insertSDisabledMilitiaman(SDisabledMilitiaman sDisabledMilitiaman) {
        if (sDisabledMilitiaman != null) {
            sDisabledMilitiamanMapper.insertSDisabledMilitiaman(sDisabledMilitiaman);
        }
        return 0;
    }

    /**
     * 修改伤残民兵民工信息
     *
     * @param sDisabledMilitiaman
     *         伤残民兵民工信息
     *
     * @return 结果
     */
    @Override
    public int updateSDisabledMilitiaman(SDisabledMilitiaman sDisabledMilitiaman) {
        return sDisabledMilitiamanMapper.updateSDisabledMilitiaman(sDisabledMilitiaman);
    }

    /**
     * 批量删除伤残民兵民工信息
     *
     * @param ids
     *         需要删除的伤残民兵民工信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDisabledMilitiamanByIds(Long[] ids) {
        return sDisabledMilitiamanMapper.deleteSDisabledMilitiamanByIds(ids);
    }

    /**
     * 删除伤残民兵民工信息信息
     *
     * @param id
     *         伤残民兵民工信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDisabledMilitiamanById(Long id) {
        return sDisabledMilitiamanMapper.deleteSDisabledMilitiamanById(id);
    }
}
